# from django.shortcuts import render

# Create your views here.
# from rest_framework import generics
# from .models import Student
# from .serializers import StudentSerializer

# class StudentListCreateView(generics.ListCreateAPIView):
#     queryset = Student.objects.all()
#     serializer_class = StudentSerializer

# class StudentRetrieveUpdateDestroyView(generics.RetrieveUpdateDestroyAPIView):
#     queryset = Student.objects.all()
#     serializer_class = StudentSerializer

from typing import Any

from django.db.models import Avg, Count, Max, Min, Sum
from django.http import JsonResponse
from .models import Student
from django.views.decorators.csrf import csrf_exempt
from django.db.models import Q
# import json


@csrf_exempt
def student_list_create(request):
    # 当我们只需要获取模型对象中的部分字段（而不是整个模型实例）时，会使用values()。
    # 这样返回的是一个字典（QuerySet of dicts），而不是模型对象，可以更方便地序列化或直接用于JsonResponse等场景。
    # students = list(Student.objects.values())
    # students = list(Student.objects.values().order_by('id'))
    # students = list(Student.objects.values().order_by('-id'))
    # students = list(Student.objects.values().order_by('name', 'id'))
    # students = list(Student.objects.values().filter(id=1))
    # students = list(Student.objects.values().filter(name='学生B001'))
    # students = list(Student.objects.filter(id__gt=10).values())
    # students = list(Student.objects.filter(id__gte=10).values())
    # students = list(Student.objects.filter(id__lt=10).values())
    # students = list(Student.objects.filter(id__lte=10).values())
    # students = list(Student.objects.filter(id__in=[1, 2, 3]).values())

    # id__not_in 是无效的查询表达式，应该使用 id__in 取反的方法, 例如 ~Q(id__in=[1,2,3])
    # students = list(Student.objects.filter(~Q(id__in=[1, 2, 3])).values())

    # 这行代码的意思是：查询所有id为null的学生（实际上id字段作为主键一般不会为null，这条语句通常不会返回任何结果）。
    # students = list(Student.objects.filter(id__isnull=True).values())
    # students = list(Student.objects.filter(id__isnull=False).values())

    # students = list(Student.objects.values('id', 'name'))
    # students = list[Any](Student.objects.values().only('id', 'name'))




    # 模糊查询姓名中包含"学生B"的学生
    # 精确匹配（区分大小写）
    # students = list(Student.objects.filter(name__exact='学生B001').values())
    # 返回对象
    # students = Student.objects.filter(name__exact='学生B001').values().first()

    # 精确匹配（不区分大小写）
    # students = list(Student.objects.filter(name__iexact='学生b001').values())

    # 以“学生B”开头（区分大小写）
    # students = list(Student.objects.filter(name__startswith='学生B').values())

    # 以“学生B”开头（不区分大小写）
    # students = list(Student.objects.filter(name__istartswith='学生b').values())

    # 以“学生B”结尾（区分大小写）
    # students = list(Student.objects.filter(name__endswith='学生B').values())

    # 以“学生B”结尾（不区分大小写）
    # students = list(Student.objects.filter(name__iendswith='学生B').values())

    # 包含（区分大小写）
    # students = list(Student.objects.filter(name__contains='学生B').values())

    # 包含（不区分大小写）（原始写法保留）
    # students = list(Student.objects.filter(name__icontains='学生b').values())
    

    # BETWEEN ... AND ... 区间查询
    # 查询id在10到20之间（包含10和20）的学生
    # students = list(Student.objects.filter(id__gte=10, id__lte=20).values())
    # students = list(Student.objects.filter(id__range=[10, 20]).values())
    
    # 排除查询
    # students = list(Student.objects.exclude(id=10).values())

    # 复合查询
    # students = list(Student.objects.filter(Q(age__gte=18) & Q(status=1)).values())

    # 去重查询
    # students = list(Student.objects.values('status').distinct())
    # students = list(Student.objects.values('status').distinct())
    # 返回 name 和 id 字段的去重结果（以 name, id 组合去重）
    # students = list(Student.objects.values('id', 'name').distinct())

    # 聚合查询
    # Student.objects.aggregate(Avg('age')) 返回的是一个字典，不是一个可迭代对象，所以不能直接用 list() 包裹
    # {
    #   "age__avg": 20.33
    # }   
    # students = Student.objects.aggregate(Avg('age'))
    # students = Student.objects.aggregate(Max('age'))
    # students = Student.objects.aggregate(Min('age'))
    # students = Student.objects.aggregate(Sum('age'))
    # students = Student.objects.aggregate(Count('age'))
    # students = Student.objects.aggregate(Avg('age'), Max('age'), Min('age'), Sum('age'), Count('age'))

    # 分组查询
    # students = list(
        # Student.objects.values('status').annotate(count=Count('id'))
        # Student.objects.values('status').annotate(
        #     count=Count('*'),
        #     max_age=Max('age'))
        # )
        # 这里是分成 status 和 age 两个字段为一组来分组统计,不过感觉没啥意义。
        # Student.objects.values('status', 'age').annotate(
        #     count=Count('*'),
        #     max_age=Max('age'))
    # )

    # 分页查询
    # Django 的 QuerySet 没有 paginate 方法，分页可以这样实现：
    # page = 1
    # per_page = 10
    # start = (page - 1) * per_page
    # end = page * per_page
    # students = list(
    #     Student.objects.values()
    #     .order_by('id')[start:end]
    # )
    # 更简便的分页写法可以直接用切片，不需要额外定义 start 和 end 变量
    students = list(Student.objects.values().order_by('id')[0:10])
    # students = list(Student.objects.values().order_by('id')[19:30])

    return JsonResponse(students, safe=False)

def student_rud(request, pk):
    return request